package com.service.infoEntry.service.impl;

import com.service.config.utils.DateUtils;
import com.service.config.utils.IdUtil;
import com.service.config.utils.ModelMapperUtil;
import com.service.infoEntry.dao.CommentDao;
import com.service.infoEntry.dto.CommentDto;
import com.service.infoEntry.entity.CommentInfo;
import com.service.infoEntry.service.CommentService;
import com.service.infoEntry.service.UserInfoService;
import com.service.infoEntry.vo.CommentVo;
import com.service.infoEntry.vo.UserVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentDao commentDao;
    @Resource
    private UserInfoService userService;

    @Override
    public void addComment(CommentDto commentDto) {
        CommentInfo commentInfo = ModelMapperUtil.strictMap(commentDto, CommentInfo.class);
        commentInfo.setId(IdUtil.getStringId());
        commentInfo.setAddTime(DateUtils.getNowDate());
        UserVo userVo = userService.queryByUserId(commentDto.getUserId());
        commentInfo.setUsername(userVo.getUsername());
        commentDao.addComment(commentInfo);
    }

    @Override
    public List<CommentVo> queryAllCommentByTroubleId(String troubleId, String userId) {
        List<CommentInfo> commentInfoList = commentDao.queryAllCommentByTroubleId(troubleId);
        List<CommentVo> commentVoList = new ArrayList<>();
        CommentVo commentVo;
        UserVo userVo;
        if(null != commentInfoList && commentInfoList.size()>0)
        {
            for (CommentInfo commentInfo : commentInfoList) {
                commentVo = ModelMapperUtil.strictMap(commentInfo,CommentVo.class);
                userVo = userService.queryByUserId(commentInfo.getUserId());
                if(userId.equals(commentInfo.getUserId()))
                {
                    userVo.setCurrentUser(true);
                }
                if(userVo.getFullName()==null)
                {
                    userVo.setFullName("");
                }
                commentVo.setUserVo(userVo);
                commentVoList.add(commentVo);
            }
        }
        return commentVoList;
    }
}
